﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpShare.Storage {
    public delegate IStorageDataProvider StorageFileOpenHandler(StorageFileAccess access, StorageFileAccess share);

    public class StorageFile : StorageItem, IStorageFile {
        private StorageFileOpenHandler _openHandler;

        public StorageFile(string name, IStorageContainer parent, StorageFileOpenHandler openHandler = null)
            : base(name, parent) {
                _openHandler = openHandler;
        }

        public long Length { get; set; }

        protected virtual IStorageDataProvider Open(StorageFileAccess access, StorageFileAccess share) {
            if (_openHandler != null) {
                return _openHandler(access, share);
            }

            throw new StorageItemUnauthorizedException();
        }

        #region IStorageFile Members

        long IStorageFile.Length {
            get { return this.Length; }
        }

        IStorageDataProvider IStorageFile.Open(StorageFileAccess access, StorageFileAccess share) {
            return this.Open(access, share);
        }

        #endregion
    }
}
